using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEngine;
using UnityEngine.SceneManagement;

public enum LoadType
{
    Ready,
    Start,
    Loading,
    Done,
    Error,
}
public class SceneController : Singleton<SceneController>
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
        
    public LoadType loadType = LoadType.Ready;
    public LoadType unloadType = LoadType.Ready;
    private string _current_gameScene = "StartGameScene";
    private readonly string _uiSceneName = "UIScene";
    private readonly string _startGameSceneName = "StartGameScene";
    public void LoadUI()
    {
        SceneManager.LoadScene(_uiSceneName, LoadSceneMode.Additive);
    }
    
    public void LoadStartGame()
    {
        SceneManager.LoadScene(_startGameSceneName, LoadSceneMode.Additive);
        _current_gameScene = _startGameSceneName;
    }

    public string GetCurrentGameSceneName()
    {
        for (int i = 0; i < SceneManager.loadedSceneCount; i++)
        {
            var sceneName = SceneManager.GetSceneAt(i).name;
            if (sceneName != "SampleScene" && sceneName != _uiSceneName)
            {
                _current_gameScene = sceneName;
                return sceneName;
            }
            
        }
        
        return "";
    }
    public void ChangeGameScene(string sceneName)
    {
        
        StartCoroutine(ChangeGameSceneAsync(sceneName));
        
    }

    public IEnumerator ChangeGameSceneAsync(string sceneName)
    {
        LoadPanel.instance.Activate();
        loadType = LoadType.Ready;
        unloadType = LoadType.Ready;
        yield return new WaitForSeconds(LoadPanel.instance.delay_time);
        StartCoroutine(UnloadSceneAsync(_current_gameScene));
        StartCoroutine(Load(sceneName));
        while (true)
        {
            if (loadType == LoadType.Done && unloadType == LoadType.Done)
            {
                MyLog.instance.Log("切换场景成功");
                _current_gameScene = sceneName;
                LoadPanel.instance.DeActivate();
                break;
                
            }
            else if (unloadType == LoadType.Error)
            {
                MyLog.instance.Log("卸载场景出错",LogType.Error);
                break;
            }
            else if (loadType == LoadType.Error)
            {
                MyLog.instance.Log("加载场景出错",LogType.Error);
                break;
            }
            yield return null;
        }
        
        
       
    }
    public void LoadGameSceneAsync(string sceneName)
    {
        StartCoroutine(Load(sceneName));
        
    }
    
    public IEnumerator UnloadSceneAsync(string sceneName)
    {
        Scene scene = SceneManager.GetSceneByName(sceneName);
 
        if (!scene.isLoaded)
        {
            unloadType = LoadType.Error;
            yield break;
        }

        unloadType = LoadType.Start;
        AsyncOperation async = SceneManager.UnloadSceneAsync(scene);
        if (async==null)
        {
            unloadType = LoadType.Error;
            yield break;
        }
        unloadType = LoadType.Loading;
        while (async.isDone == false)
        {
            yield return new WaitForSeconds(0.1f);
        }
        unloadType = LoadType.Done;
        yield return null;
    }
    
    IEnumerator Load(string sceneName)
    {
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
        loadType = LoadType.Start;
        if (asyncOperation != null)
        {
            // asyncOperation.allowSceneActivation = false; // 这里限制了跳转

            loadType = LoadType.Loading;
            while (asyncOperation.isDone == false)
            {
                yield return new WaitForSeconds(0.1f);
            }

            // asyncOperation.allowSceneActivation = true; // 这里打开限制
            loadType = LoadType.Done;
            yield return null;

           
        }
        else
        {
            loadType = LoadType.Error;
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
